<?php
declare (strict_types=1);

namespace app\topic\service\admin;

use app\common\service\BaseService;
use app\topic\logic\admin\TopicComment as TopicCommentLogic;
use eduline\admin\libs\pageform\FormItem;
use eduline\admin\libs\pagelist\ListItem;
use eduline\admin\page\PageList;
use think\annotation\Inject;
use think\facade\Request;

class TopicComment extends BaseService
{

    /**
     * @Inject()
     *
     * @var TopicCommentLogic
     */
    protected $commentLogic;

    /**
     * 课程评论列表
     *
     * @param int $courseId
     * @return \eduline\admin\page\interfaces\think\Response
     * @throws \think\db\exception\DbException
     */
    public function commentList(int $topicId)
    {
        // 搜索功能
        $map   = [
            'topic_id' => $topicId
        ];
        $phone = Request::get('phone/s', '');
        $data  = $this->commentLogic->getList($map, $phone)->bindAttr('user', ['avatar_url', 'nick_name', 'phone'])->hidden(['user']);
        // 定义字段
        $keyList = [
            'user'        => ListItem::make('custom')->title('用户'),
            'content'     => ListItem::make()->title('评论内容'),
            'create_time' => ListItem::make()->title('评论时间')
        ];

        $searchKey = [
            'phone' => FormItem::make()->title(false)->placeholder('学员手机号')->maxlength(30),
        ];

        // 设置表单
        $list = new PageList();
        // 表单字段
        $list->pageKey   = $keyList;
        $list->searchKey = $searchKey;
        $list->datas     = $data;

        return $list->send();
    }

    /**
     * 删除评论
     *
     * @param int $topicId
     * @param int $commentId
     * @return \think\Response
     */
    public function deleteComment(int $topicId, int $commentId)
    {
        $this->commentLogic->deleteComment($topicId, $commentId);

        return $this->parseToData([], 1, '删除成功');
    }
}